#!/bin/bash

################################################################
##
##   MySQL Database To Amazon S3
##   Written By: YONG MOOK KIM
################################################################

NOW=$(date +"%Y-%m-%d")

BACKUP_DIR="/data/backup/mysql"
MYSQL_HOST="dev-common-node1"
MYSQL_PORT="3306"
MYSQL_USER="root"
MYSQL_PASSWORD='password'
#DATABASE_NAME="YOUR_DB_NAME"

AMAZON_S3_BUCKET="s3://mysql-backup"
AMAZON_S3_BIN="/usr/local/bin/aws"

#################################################################

test -d ${BACKUP_DIR}/${NOW} ||\
mkdir -p ${BACKUP_DIR}/${NOW}

backup_mysql(){
    local DATABASE_NAME="$1"
    /usr/bin/mysqldump --default-character-set=utf8 -h${MYSQL_HOST} -P${MYSQL_PORT} -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" ${DATABASE_NAME} |\
            gzip  > ${BACKUP_DIR}/${NOW}/${DATABASE_NAME}-${NOW}.sql.gz
}

# backup any folders?
backup_files(){

        tar -cvzf ${BACKUP_DIR}/backup-files-${NOW}.tar.gz ${FOLDERS_TO_BACKUP[@]}

}

#upload_s3(){
#        local DIR="$1"
#        source /etc/profile.d/aws_env.sh
#        ${AMAZON_S3_BIN} s3 cp ${DIR} ${AMAZON_S3_BUCKET}/${DIR} --recursive
#}

dbs=(
deepxin_account
deepxin_merchant
deepxin_notify
deepxin_payment
nacos_config
xxl_job
)

for db in "${dbs[@]}"
do
        backup_mysql ${db}
done
